package com.example.common.vo.cart;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author zyred
 * @since v 0.1
 **/
@Setter
@Getter
@Accessors(chain = true)
public class CartVo {

    private List<CartItemVo> carts;
    /** 商品总数量 **/
    private Integer countNum;
    /** 商品总类型 **/
    private Integer countType;
    /** 总金额，不包含减免金额 **/
    private BigDecimal totalAmount;
    /** 减免金额 **/
    private BigDecimal reduce = BigDecimal.ZERO;

    public Integer getCountNum() {
        return (int) this.carts.stream().mapToInt(CartItemVo::getCount).summaryStatistics().getSum();
    }

    public Integer getCountType() {
        return this.carts.size();
    }

    public BigDecimal getTotalAmount() {
        // 计算商品总价
        BigDecimal amount = this.carts.stream()
                .filter(CartItemVo::isCheck)
                .map(CartItemVo::getTotalPrice)
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        // 计算商品总价减优惠总价
        return amount.subtract(this.reduce);
    }
}
